package com.gjj.abox.support;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Locale;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.gjj.abox.constant.FileTypes;
import com.gjj.abox.database.R;
import com.gjj.abox.entity.FileInfo;


public class FileChooserAdapter extends BaseAdapter {

	private ArrayList<FileInfo> mFileLists;
	private LayoutInflater mLayoutInflater = null;


	public FileChooserAdapter(Context context, ArrayList<FileInfo> fileLists) {
		super();
		mFileLists = fileLists;
		mLayoutInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mFileLists.size();
	}

	@Override
	public FileInfo getItem(int position) {
		// TODO Auto-generated method stub
		return mFileLists.get(position);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = null;
		ViewHolder holder = null;
		if (convertView == null || convertView.getTag() == null) {
			view = mLayoutInflater.inflate(R.layout.filechooser_gridview_item,
					null);
			holder = new ViewHolder(view);
			view.setTag(holder);
		} else {
			view = convertView;
			holder = (ViewHolder) convertView.getTag();
		}

		FileInfo fileInfo = getItem(position);
		//TODO

		holder.tvFileName.setText(fileInfo.getFileName());

		if(fileInfo.isDirectory()){      //文件夹
			holder.imgFileIcon.setImageResource(R.mipmap.ic_folder);
			holder.tvFileName.setTextColor(Color.BLACK);
		}
		else if(fileInfo.isPPTFile()){   //PPT文件
			holder.imgFileIcon.setImageResource(R.mipmap.ic_ppt);
			holder.tvFileName.setTextColor(Color.BLACK);
		}
		else if(fileInfo.isVoiceFile()){   //声音文件
			holder.imgFileIcon.setImageResource(R.mipmap.ic_voice);
			holder.tvFileName.setTextColor(Color.BLACK);
		}
		else if(fileInfo.isTxtFile()){   //TXT文件
			holder.imgFileIcon.setImageResource(R.mipmap.ic_txt);
			holder.tvFileName.setTextColor(Color.BLACK);
		}
		else if(fileInfo.isExcelFile()){   //EXCEL文件
			holder.imgFileIcon.setImageResource(R.mipmap.ic_excel);
			holder.tvFileName.setTextColor(Color.BLACK);
		}
		else if(fileInfo.isMovieFile()){   //MOV文件
			holder.imgFileIcon.setImageResource(R.mipmap.ic_mov);
			holder.tvFileName.setTextColor(Color.BLACK);
		}
		else if(fileInfo.isWordFile()){   //WORD文件
			holder.imgFileIcon.setImageResource(R.mipmap.ic_word);
			holder.tvFileName.setTextColor(Color.BLACK);
		}
		else if(fileInfo.isPictureFile()){   //Picture文件
			holder.imgFileIcon.setImageResource(R.mipmap.ic_picture);
			holder.tvFileName.setTextColor(Color.BLACK);
		}
		else if(fileInfo.isZipFile()){   //ZIP文件
			holder.imgFileIcon.setImageResource(R.mipmap.ic_zip);
			holder.tvFileName.setTextColor(Color.BLACK);
		}
		else if(fileInfo.isApkFile()){   //APK文件
			holder.imgFileIcon.setImageResource(R.mipmap.ic_apk);
			holder.tvFileName.setTextColor(Color.BLACK);
		}else if(fileInfo.isDBFile()){
			holder.imgFileIcon.setImageResource(R.mipmap.ico_db);
			holder.tvFileName.setTextColor(Color.BLACK);
		}
		else {                           //未知文件
			holder.imgFileIcon.setImageResource(R.mipmap.ic_unkonw);
			holder.tvFileName.setTextColor(Color.GRAY);
		}
		return view;
	}

	static class ViewHolder {
		ImageView imgFileIcon;
		TextView tvFileName;

		public ViewHolder(View view) {
			imgFileIcon = (ImageView) view.findViewById(R.id.imgFileIcon);
			tvFileName = (TextView) view.findViewById(R.id.tvFileName);
		}
	}

}
